﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace hbConv
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string b2h(byte[] bytes)
        {
            StringBuilder s = new StringBuilder();
            foreach(byte b in bytes)
            {
                s.AppendFormat("{0:X2}", b);
            }
            return s.ToString();
        }
        private string b2Text(byte[] bytes)
        {
            StringBuilder s = new StringBuilder();
            foreach (byte b in bytes)
            {
                s.AppendFormat("{0:X2} ", b);
            }
            return s.ToString();
        }
        private string s2h(string b)
        {
            StringBuilder h = new StringBuilder();
            foreach(char c in b)
            {
                h.AppendFormat("{0:X2}", Convert.ToUInt32(c));
            }
            return h.ToString();
        }
        private string h2s(string h)
        {
            StringBuilder b = new StringBuilder();
            while (h.Length > 0)
            {
                b.Append(Convert.ToChar(Convert.ToUInt16(h.Substring(0, 2), 16)));
                h = h.Substring(2);
            }
            return b.ToString();
        }

        private byte[] s2b(string h)
        {
            //return System.Text.Encoding.ASCII.GetBytes(h2s(h));
            byte[] bytes = new byte[h.Length];
            for(int i=0; i<h.Length; ++i)
            {
                bytes[i] = Convert.ToByte(h[i]);
            }
            return bytes;
        }

        private void btn_h2b_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "文本文件|*.txt";
            if(dlg.ShowDialog() == DialogResult.OK)
            {
                StreamReader file = File.OpenText(dlg.FileName);
                string text = file.ReadToEnd();
                file.Close();
                text = text.Replace("\r", "");
                text = text.Replace("\n", "");
                text = text.Replace(" ", "");
                text = text.Replace("0x", "");

                try
                {
                    string s = h2s(text);
                    byte[] b = s2b(s);
                    FileStream fs = File.OpenWrite(dlg.FileName + ".bin");
                    fs.Write(b, 0, b.Length);
                    fs.Close();
                }
                catch (System.FormatException ex)
                {
                    MessageBox.Show("Convert Failed, Error: " + ex.Message);
                }
                
            }
        }

        private void btn_b2h_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "所有文件|*.*";
            if(dlg.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(dlg.FileName, FileMode.Open);
                BinaryReader file = new BinaryReader(fs);
                byte[] buff = new byte[fs.Length];
                file.Read(buff, 0, (int)fs.Length);
                fs.Close();
                
                string hex = b2Text(buff);
                int offset = 0;
                using (TextWriter writer = new StreamWriter(dlg.FileName + ".txt", false))
                {
                    while (offset < hex.Length)
                    {
                        string sLine = hex.Substring(offset);
                        if (sLine.Length > 48)
                            sLine = sLine.Substring(0, 48);
                        offset += sLine.Length;
                        writer.WriteLine(sLine);
                    }
                   
                }
            }
        }
    }
}
